/**
 * 
 */
package demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import demo.dao.UserRepository;
import demo.domain.User;
import demo.service.UserService;

/**
 * @author mdegrugi
 * 
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    /**
     * Count the number of users
     * 
     * @return long
     */
    @Override
    public long countUsers() {
        return userRepository.count();
    }

    /**
     * Return all users
     * 
     * @return List
     */
    @Override
    public List<User> findAllUsers() {
        return userRepository.findAll();
    }

    @Override
    @Cacheable(value = "users", key = "#login")
    public User findByLogin(final String login) {
        return userRepository.findByLogin(login);
    }

    @Override
    public User create(final String login, final String firstname, final String lastname) {
        final User user = new User();
        user.setFirstName(firstname);
        user.setLastName(lastname);
        user.setLogin(login);
        user.setPassword("password");
        userRepository.save(user);
        return user;
    }

}
